package com.android.djlmapp.tools.global;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;

/**
 * 软键盘相关工具类
 */
public class KeyBordUtils {
    public static void closeInputMethod(Activity mActivity, View view) {
        InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public static void showKeyboard(Activity mActivity) {
        InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (null == imm)
            return;
        if (mActivity.getCurrentFocus() != null) {
            //有焦点关闭
            imm.hideSoftInputFromWindow(mActivity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    //纪录根视图的显示高度
    private static int rootViewVisibleHeight;

    /**
     * 听软键盘的显示和隐藏
     *
     * @param activity
     * @param listener
     */
    public static void addOnSoftKeyBoardListener(Activity activity,
                                                 final OnSoftKeyBoardListener listener) {
        //activity的根视图
        final View rootView = activity.getWindow().getDecorView();

        //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        //获取当前根视图在屏幕上显示的大小
                        Rect rect = new Rect();
                        rootView.getWindowVisibleDisplayFrame(rect);
                        int visibleHeight = rect.height();
                        if (rootViewVisibleHeight == 0) {
                            rootViewVisibleHeight = visibleHeight;
                            return;
                        }
                        //根视图显示高度没有变化，可以看作软键盘显示／隐藏状态没有改变
                        if (rootViewVisibleHeight == visibleHeight) {
                            return;
                        }
                        //根视图显示高度变小超过200，可以看作软键盘显示了
                        if (rootViewVisibleHeight - visibleHeight > 200) {
                            listener.keyBoardShow();
                            rootViewVisibleHeight = visibleHeight;
                            return;
                        }
                        //根视图显示高度变大超过200，可以看作软键盘隐藏了
                        listener.keyBoardHide();
                        rootViewVisibleHeight = visibleHeight;
                    }
                });
    }

    public interface OnSoftKeyBoardListener {
        void keyBoardShow();

        void keyBoardHide();
    }
}
